package vn.mobiistar.videoapp.loader;

import java.util.ArrayList;

import org.json.JSONObject;

import vn.mobiistar.videoapp.other.Category;
import vn.mobiistar.videoapp.other.Define;
import android.content.Context;
import android.util.Log;

public class CategoryLoader extends BaseLoader {

	// type = 1: get new video
	// type = 2: get mostview
	// type = 3: get video by catid
	public String mUrl;
	public int mType;
	public int mPage;
	public boolean mNeedNextPage;
	
	public int getType() {
		return mType;
	}
	public void setType(int mType) {
		this.mType = mType;
	}
	public CategoryLoader(Context context) {
		super(context);
	}
	@Override
	protected BaseResult getResultInstance() {
		return new Result();
	}
	
	// tra ve mot doi tuong
	// xac dinh request
	@Override
	protected UpdateTask getUpdateTask() {
		return new CategoryUpdateTask();
	}

	@Override
	protected BaseResult parseResult(JSONObject paramJSONObject,
			BaseResult result) throws Exception {
		Result localResult = (Result) result;
		localResult.mCategory = Category.valueOf(paramJSONObject);
		return localResult;
	}

	// update task
	private class CategoryUpdateTask extends BaseLoader.UpdateTask {

		private boolean mIsAppend;

		@Override
		protected Request getRequest() {
			switch(CategoryLoader.this.getType()){
				case Define.SERVICE_GET_ALLCATE:
					mUrl = Define.CATEGORYLOADER_URL;
					break;
				case Define.SERVICE_GET_CATEGORYFORMENU:
					mUrl = Define.CATEGORYFORMENU_URL;
					break;
			}
				Log.v("url",mUrl);
				return new Request(mUrl);
		}
		protected BaseResult onDataLoaded(BaseResult oldresult, BaseResult newresult){
			BaseResult localResult = null;
			if(newresult != null && (((Result)newresult).mCategory != null) && (!((Result)newresult).mCategory.isEmpty())){
				CategoryLoader.this.mNeedNextPage = true;
				localResult = newresult;
				mResult = newresult.shallowClone();
				if(((CategoryLoader.Result)oldresult).mCategory != null && ((CategoryLoader.Result)newresult).mCategory != null) 
				if(((CategoryLoader.Result)oldresult).mCategory.size() >0 && ((CategoryLoader.Result)newresult).mCategory.size() >0){
					localResult = merge((CategoryLoader.Result)oldresult, (CategoryLoader.Result)newresult);
				}
				return localResult;
			}
			return newresult;
		}

		private CategoryLoader.Result merge(CategoryLoader.Result oldresult, CategoryLoader.Result newresult){
			CategoryLoader.Result localResult = new CategoryLoader.Result();
			localResult.mCategory = new ArrayList<Category>();    
			if (oldresult != null)
			if (newresult != null)
				localResult.mCategory.addAll(newresult.mCategory);
			return localResult;
		}
	}

	// Result
	public static final class Result extends BaseResult {
		public ArrayList<Category> mCategory;
		protected int getCount() {
			int count = 0;
			if (this.mCategory != null)
				count = this.mCategory.size();
			return count;
		}
		public BaseResult shallowClone() {
			Result localResult = new Result();
			localResult.mCategory = this.mCategory;
			return localResult;
		}
	}
	@Override
	public void reload() {
		if (!isLoading())
		{
			this.mPage = 0;
			this.mNeedNextPage = false;
			super.reload();
		}
	}


	public void nextPage() {
		if (this.mNeedNextPage)
			this.mPage = (1 + this.mPage);
	}

}
